package com.car.service.impl;

import com.car.mapper.EmpMapper;
import com.car.mapper.RepairAppointmentMapper;
import com.car.pojo.Emp;
import com.car.pojo.EmpQueryParam;
import com.car.pojo.PageResult;
import com.car.pojo.RepairAppointment;
import com.car.pojo.RepairAppointmentQueryParam;
import com.car.service.RepairAppointmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 故障预约Service实现类
 */
@Service
public class RepairAppointmentServiceImpl implements RepairAppointmentService {
    
    @Autowired
    private RepairAppointmentMapper repairAppointmentMapper;
    
    @Autowired
    private EmpMapper empMapper;
    
    @Override
    public PageResult<RepairAppointment> page(RepairAppointmentQueryParam param) {
        // 确保页码至少为1，避免计算出负的偏移量
        int pageNum = Math.max(1, param.getPage());
        // 计算偏移量
        param.setPage((pageNum - 1) * param.getPageSize());
        
        // 查询数据和总记录数
        List<RepairAppointment> list = repairAppointmentMapper.page(param);
        Long total = repairAppointmentMapper.count(param);
        
        // 返回分页结果
        return new PageResult<>(total, list);
    }
    
    @Override
    public RepairAppointment getInfo(Long id) {
        return repairAppointmentMapper.getById(id);
    }
    
    @Override
    public void updateStatus(Long id, String status) {
        repairAppointmentMapper.updateStatus(id, status, null);
    }
    
    @Override
    public void updateStatus(Long id, String status, String empName) {
        // 更新预约状态和维修员信息
        repairAppointmentMapper.updateStatus(id, status, empName);
        
        // 如果分配了维修员，将该维修员的状态更新为"忙碌"
        if (empName != null && !empName.isEmpty()) {
            // 查询该维修员的信息
            EmpQueryParam empQueryParam = new EmpQueryParam();
            empQueryParam.setName(empName);
            List<Emp> empList = empMapper.list(empQueryParam);
            
            if (empList != null && !empList.isEmpty()) {
                Emp emp = empList.get(0);
                emp.setStatus("忙碌");
                empMapper.updateById(emp);
            }
        }
    }
    
    @Override
    public void delete(List<Long> ids) {
        repairAppointmentMapper.deleteBatch(ids);
    }
    
    @Override
    public List<RepairAppointment> list(RepairAppointmentQueryParam param) {
        return repairAppointmentMapper.listAll(param);
    }
    
    @Override
    public void add(RepairAppointment appointment) {
        appointment.setStatus("待确认");
        // 添加故障预约
        repairAppointmentMapper.add(appointment);
    }
    
    @Override
    public List<RepairAppointment> listByUserId(Long userId) {
        return repairAppointmentMapper.listByUserId(userId);
    }
    
    @Override
    public Integer countByUserId(Long userId) {
        return repairAppointmentMapper.countByUserId(userId);
    }
    
    @Override
    public List<Map<String, Object>> getRepairStaff() {
        return repairAppointmentMapper.getRepairStaff();
    }
    
    @Override
    public List<RepairAppointment> listByEmpName(String empName) {
        return repairAppointmentMapper.listByEmpName(empName);
    }
    
    @Override
    public Integer countConfirmedByEmpName(String empName) {
        return repairAppointmentMapper.countConfirmedByEmpName(empName);
    }
    
    @Override
    public Integer countCompletedByEmpName(String empName) {
        return repairAppointmentMapper.countCompletedByEmpName(empName);
    }

}